package com.kalaqia.base.ui.list;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kalaqia.base.ui.BaseFragment;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kalaqia 421407393@qq.com
 */
public abstract class BaseListFragment<M extends BaseListContract.Model
        , P extends BaseListContract.Presenter>
        extends BaseFragment implements BaseListContract.View<M> {

    protected final List<Object> lists = new ArrayList<>();
    protected SlimAdapter mAdapter;
    protected RecyclerView mRecyclerView;

    protected List<M> modelList;

    public void setModelList(List<M> modelList) {
        this.modelList = modelList;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_list;
    }

    @Override
    protected void initView(View view) {
        if (mRecyclerView == null) {
            mRecyclerView = view.findViewById(R.id.id_base_recycler_view);
        }
        mRecyclerView.setFocusable(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        SlimAdapterEx adapterEx = SlimAdapter.create(SlimAdapterEx.class);

        if (mStateUtils == null && mStateUtilsFactory != null) {
            mStateUtils = mStateUtilsFactory.createStateUtils();
        }
//        if (isSupportErrorState())
        if (mStateUtils != null) {
            adapterEx.enableEmptyView(mStateUtils.build());
        }
        mAdapter = adapterEx
                .updateData(lists)
                .attachTo(mRecyclerView);
        provideAdapter();
        initRefreshLayout();
    }

    protected void initRefreshLayout() {
        refreshData();
    }

    protected IStateUtils mStateUtils;

    public void setStateUtils(IStateUtils mStateUtils) {
        this.mStateUtils = mStateUtils;
    }

    /**
     * init adapter
     */
    protected abstract void provideAdapter();

    protected RecyclerView.AdapterDataObserver observer;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (observer != null) {
            mAdapter.unregisterAdapterDataObserver(observer);
        }
        observer = null;
        if (listPresenter != null) {
            listPresenter = null;
        }
    }

    protected M res;

    public void setRes(M res) {
        this.res = res;
    }

    protected int curPage = 1;

    protected P listPresenter;

    public void refreshData() {
        if (listPresenter != null) {
            listPresenter.refreshData(curPage);
        }
    }

    @Override
    public void showResult(List<M> results) {
        if (results == null) {
            return;
        }
        if (results.size() == 0 && curPage == 1 && mStateUtils != null) {
            mStateUtils.showNoData();
        }
        if (curPage == 1) {
            lists.clear();
        }
        int start = lists.size();
        lists.addAll(results);
        if (curPage == 1) {
            mAdapter.notifyDataSetChanged();
        } else {
            mAdapter.notifyItemRangeInserted(start, start + results.size());
        }

    }

    @Override
    public void setAllPage(int page) {

    }

    @Override
    public void setState(String msg) {
        if (mStateUtils == null) {
            return;
        }
        mStateUtils.showLoadError(msg);
    }


    private static StateUtilsFactory mStateUtilsFactory;

    public static void setStateUtilsFactory(StateUtilsFactory mStateUtilsFactory) {
        BaseListFragment.mStateUtilsFactory = mStateUtilsFactory;
    }
}
